package com.zhu.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.Objects;
import java.util.function.Supplier;

/**
 * 功能描述
 * @author JoyBoyZH
 * @date 2022/5/30
 * @return
 */
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("start insert fill ....");
        log.info(String.valueOf(LocalDateTime.now()));
        this.setFieldValByName("createTime",new Date(), metaObject);
        this.strictInsertFill(metaObject, "isUserManage", Boolean.class, false);
        this.strictInsertFill(metaObject, "isGoodsManage", Boolean.class, false);
        this.strictInsertFill(metaObject, "isInventoryManage", Boolean.class, false);
        this.strictInsertFill(metaObject, "isWarehouseManage", Boolean.class, false);
        this.strictInsertFill(metaObject, "deleted", Boolean.class, false);

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update fill ....");
//        this.strictUpdateFill(metaObject, MODIFY_TIME, LocalDateTime.class, LocalDateTime.now());
//        log.info(String.valueOf(LocalDateTime.now()));
    }

//    @Override
//    public MetaObjectHandler strictFillStrategy(MetaObject metaObject, String fieldName, Supplier<?> fieldVal) {
//        //每次填充，直接使用填充值
//        //获取字段值，封装到Object对象中
//        Object obj = fieldVal.get();
//        //判断值是否为空
//        if (Objects.nonNull(obj)) {
//            metaObject.setValue(fieldName, obj);
//        }
//        return this;
//    }
}
